package com.ptswitch.material.pages.bom;

import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry5.EventConstants;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.ptswitch.material.model.material.Bom;
import com.ptswitch.material.model.material.BomMaterial;
import com.ptswitch.material.model.material.Material;
import com.ptswitch.material.pages.base.PageBase;
import com.ptswitch.material.service.BomService;
import com.ptswitch.material.util.security.PermissionUtil;

public class ViewBom extends PageBase {
    @Inject
    private BomService bomSer;
    @Property
    private Bom bom;
    private long id;
    @Property
    private List<BomMaterial> emptyCodeBm = new ArrayList<BomMaterial>();
    @Property
    private List<BomMaterial> nullMaterialBm = new ArrayList<BomMaterial>();
    @Property
    private List<BomMaterial> notPublishBm = new ArrayList<BomMaterial>();
    @Property
    private List<BomMaterial> notSuggestBm = new ArrayList<BomMaterial>();
    @Property
    private List<BomMaterial> stopUseBm = new ArrayList<BomMaterial>();
    @Property
    private List<BomMaterial> disagreeBm = new ArrayList<BomMaterial>();
    @Property
    private List<BomMaterial> goodBm = new ArrayList<BomMaterial>();

    Object withInit(long id) {
        init(id);
        return this;
    }

    @OnEvent(value = EventConstants.ACTIVATE)
    Object init(long id) {
        checkPermitted(PermissionUtil.BomView);
        this.id = id;
        return null;
    }

    @SetupRender
    void display() {
        bom = bomSer.findById(id, BomService.Init_BomMaterials);
        if (bom == null) {
            return;
        }
        emptyCodeBm.clear();
        nullMaterialBm.clear();
        notPublishBm.clear();
        notSuggestBm.clear();
        stopUseBm.clear();
        disagreeBm.clear();
        goodBm.clear();
        for (BomMaterial bm : bom.getBomMaterials()) {
            if (bm.getCode().isEmpty()) {
                emptyCodeBm.add(bm);
            } else if (bm.getMaterial() == null) {
                nullMaterialBm.add(bm);
            } else if (bm.getMaterial().getState().contains(Material.State_UnPublish)) {
                notPublishBm.add(bm);
            } else if (bm.getMaterial().getState().contains(Material.State_UnRecommend)) {
                notSuggestBm.add(bm);
            } else if (bm.getMaterial().getState().contains(Material.State_StopUse)) {
                stopUseBm.add(bm);
            } else if (!bm.getMaterial().getPcbDecal().equals(bm.getFootprint())) {
                disagreeBm.add(bm);
            } else {
                goodBm.add(bm);
            }
        }
    }

    @OnEvent(value = EventConstants.PASSIVATE)
    Object onPassivate() {
        return id;
    }
}
